package com.grm.direct;

import com.grm.util.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;

public class Provider {

    public static void main(String[] args) {
        sendMessage();
    }

    public static void sendMessage() {
        try {
            //建立连接
            Connection connection = RabbitMqUtils.getConnection();

            //建立通道
            Channel channel = connection.createChannel();

            /*
             * desc: 通过通道声明交换机
             *
             * queue 交换机名称
             * durable 路由模式-直连
             */
            channel.exchangeDeclare("canal.exchange","direct");

            /*
             * desc: 发送消息
             *
             * exchange 交换机名称
             * routingKey 发往消息的队列名称
             * props 传递消息的额外设置 eg：MessageProperties.PERSISTENT_TEXT_PLAIN 消息持久化  前提是队列持久化
             * body 消息的具体内容  字节类型
             */
            String routingKey = "canal.routing.key";
            channel.basicPublish("canal.exchange", routingKey, null, ("这是direct模型基于routing key :["+routingKey+"]发送的消息").getBytes());
            RabbitMqUtils.close(channel, connection);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
